﻿#region Using Statements
using System;
using System.Collections.Generic;
#endregion

namespace DirectZZT.Shared.Core
{
    /// <summary>
    /// Provides a generic resource manager for various resources.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResourceManager<T> : IDisposable
    {
        #region Fields

        private Dictionary<string, T> _resources;
        private T _defaultResource;

        #endregion

        #region Properties

        /// <summary>
        /// Gets/sets the default resource (if any).
        /// </summary>
        public T DefaultResource
        {
            get { return _defaultResource; }
            set { _defaultResource = value; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the ResourceManager class.
        /// </summary>
        public ResourceManager()
        {
            _resources = new Dictionary<string, T>(16);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns whether or not a resource by the given identifier is registered.
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">Any of the parameters were null or empty.</exception>
        public bool ContainsResource(string identifier)
        {
            if (string.IsNullOrWhiteSpace(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            return _resources.ContainsKey(identifier);
        }

        /// <summary>
        /// Registers a resource under the given identifier.
        /// </summary>
        /// <param name="identifier"></param>
        /// <param name="resource"></param>
        /// <returns>Whether or not the resource could be registered. If this is false, then the resource did already exist in the list.</returns>
        /// <exception cref="System.ArgumentNullException">Any of the parameters were null or empty.</exception>
        public bool RegisterResource(string identifier, T resource)
        {
            if (string.IsNullOrWhiteSpace(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (resource == null)
            {
                throw new ArgumentNullException("resource");
            }

            if (_resources.ContainsKey(identifier))
            {
                return false;
            }
            _resources[identifier] = resource;
            return true;
        }

        /// <summary>
        /// Returns the resource registered under the given identifier.
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns></returns>
        public T GetResource(string identifier)
        {
            if (_resources.ContainsKey(identifier))
            {
                return _resources[identifier];
            }
            return _defaultResource;
        }

        /// <summary>
        /// Removes the given resource from the resource manager.
        /// </summary>
        /// <param name="identifier"></param>
        /// <exception cref="System.ArgumentNullException">Any of the parameters were null or empty.</exception>
        public void RemoveResource(string identifier)
        {
            if (string.IsNullOrWhiteSpace(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (_resources.ContainsKey(identifier))
            {
                _resources.Remove(identifier);
            }
        }

        /// <summary>
        /// Clears all resources (except the default resource) from the resource manager.
        /// </summary>
        public void ClearResources()
        {
            _resources.Clear();
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Disposes all resources.
        /// </summary>
        public void Dispose()
        {
            // TODO: this is the correct approach, right? it gives exceptions lateron
            // ignoring this doesn't really raise the RAM usage, but we'll keep it here
            //    foreach (T resource in _resources.Values)
            //    {
            //        resource.Dispose();
            //    }
            this.ClearResources();
            _resources = null;

            // reset default resource
            _defaultResource = default(T);
        }

        #endregion
    }
}
